import { defHttp } from '@/utils/http/axios';

// 订单接口
export interface Order {
  id: string;
  orderNumber: string;
  roomNumber: string;
  roomType: string;
  guestSource: string;
  guestName: string;
  contactPhone: string;
  idCard: string;
  payment: number;
  consumption: number;
  preAuth: number;
  checkInTime: string;
  checkOutTime: string;
  status: '未结' | '已结' | '已挂账' | '已取消';
  remarks: string;
  createTime: string;
  updateTime: string;
}

// 消费明细接口
export interface ConsumptionDetail {
  id: string;
  orderId: string;
  item: string;
  amount: number;
  quantity: number;
  unitPrice: number;
  time: string;
}

// 支付记录接口
export interface PaymentRecord {
  id: string;
  orderId: string;
  method: string;
  amount: number;
  time: string;
  status: string;
}

// 操作记录接口
export interface OperationLog {
  id: string;
  orderId: string;
  operation: string;
  operator: string;
  time: string;
  remark: string;
}

// 查询参数接口
export interface OrderQueryParams {
  keyword?: string;
  guestSource?: string;
  status?: string;
  dateRange?: string[];
  pageNum?: number;
  pageSize?: number;
}

// 订单统计接口
export interface OrderStatistics {
  total: number;
  unsettled: number;
  settled: number;
  onAccount: number;
  cancelled: number;
  sourceStats: Record<string, number>;
  totalPayment: number;
  totalConsumption: number;
}

// 获取订单列表
export function getOrders(params: OrderQueryParams) {
  return defHttp.get<{ list: Order[]; total: number }>({
    url: '/hotel/orders',
    params,
  });
}

// 获取订单详情
export function getOrderDetail(id: string) {
  return defHttp.get<Order>({
    url: `/hotel/orders/${id}`,
  });
}

// 创建订单
export function createOrder(data: Partial<Order>) {
  return defHttp.post<Order>({
    url: '/hotel/orders',
    data,
  });
}

// 更新订单
export function updateOrder(id: string, data: Partial<Order>) {
  return defHttp.put<Order>({
    url: `/hotel/orders/${id}`,
    data,
  });
}

// 删除订单
export function deleteOrder(id: string) {
  return defHttp.delete({
    url: `/hotel/orders/${id}`,
  });
}

// 办理入住
export function checkInOrder(id: string) {
  return defHttp.post({
    url: `/hotel/orders/${id}/checkin`,
  });
}

// 续住
export function extendStayOrder(id: string, days: number) {
  return defHttp.post({
    url: `/hotel/orders/${id}/extend`,
    data: { days },
  });
}

// 退房
export function checkOutOrder(id: string) {
  return defHttp.post({
    url: `/hotel/orders/${id}/checkout`,
  });
}

// 结算
export function settleOrder(id: string) {
  return defHttp.post({
    url: `/hotel/orders/${id}/settle`,
  });
}

// 取消订单
export function cancelOrder(id: string, reason: string) {
  return defHttp.post({
    url: `/hotel/orders/${id}/cancel`,
    data: { reason },
  });
}

// 获取消费明细
export function getConsumptionDetails(orderId: string) {
  return defHttp.get<ConsumptionDetail[]>({
    url: `/hotel/orders/${orderId}/consumption`,
  });
}

// 获取支付记录
export function getPaymentRecords(orderId: string) {
  return defHttp.get<PaymentRecord[]>({
    url: `/hotel/orders/${orderId}/payments`,
  });
}

// 获取操作记录
export function getOperationLogs(orderId: string) {
  return defHttp.get<OperationLog[]>({
    url: `/hotel/orders/${orderId}/logs`,
  });
}

// 获取订单统计
export function getOrderStatistics() {
  return defHttp.get<OrderStatistics>({
    url: '/hotel/orders/statistics',
  });
}

// 导出订单数据
export function exportOrders(params: OrderQueryParams) {
  return defHttp.get({
    url: '/hotel/orders/export',
    params,
    responseType: 'blob',
  });
}

// 读身份证
export function readIdCard() {
  return defHttp.post<{
    name: string;
    idCard: string;
    phone: string;
  }>({
    url: '/hotel/orders/read-idcard',
  });
}
